package com.huike.web.controller.system;

import com.huike.clues.domain.result.UserInfoAjaxResult;
import com.huike.clues.service.ISysRoleService;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.domain.entity.SysRole;
import com.huike.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.huike.clues.service.ISysRoleService;
import com.huike.common.constant.HttpStatus;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.domain.entity.SysRole;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "角色信息相关接口")
@Slf4j
@RestController
@RequestMapping("/system/role")
public class SysRoleController extends BaseController {
    @Resource
    private ISysRoleService iSysRoleService;

    @PostMapping()
    @ApiOperation("新增角色")
    public UserInfoAjaxResult insertRole(@RequestBody SysRole sysRole) {
        log.info("新增角色，操作人:{}", SecurityUtils.getUsername());
        iSysRoleService.insertRole(sysRole);
        return UserInfoAjaxResult.success();
    }

    @PutMapping()
    @ApiOperation("修改保存角色")
    public UserInfoAjaxResult updateRole(@RequestBody SysRole sysRole) {
        log.info("修改保存角色,操作人:{}，操作对象id{}", SecurityUtils.getUsername(), sysRole.getRoleId());
        iSysRoleService.updateRole(sysRole);
        return UserInfoAjaxResult.success();
    }

    @PutMapping("/changeStatus")
    @ApiOperation("状态修改")
    public UserInfoAjaxResult updateRoleStatus(@RequestBody SysRole sysRole) {
        log.info("修改保存角色,操作人:{},操作对象id{}", SecurityUtils.getUsername(), sysRole.getRoleId());
        iSysRoleService.updateRole(sysRole);
        return UserInfoAjaxResult.success();
    }

    @Autowired
    private ISysRoleService sysRoleService;

    /**
     * 根据角色ID 批量删除角色
     *
     * @param roleIds
     * @return
     */
    @DeleteMapping("/{roleIds}")
    @ApiOperation("批量删除")
    @PreAuthorize("@ss.hasPermi('system:role:remove')")
    public AjaxResult deleteRoleByRoleIds(@PathVariable Long[] roleIds) {
        log.info("批量删除:{}", roleIds);
        //获取删除的行数
        int rows = sysRoleService.deleteRoleByRoleIds(roleIds);

        return toAjax(rows);
    }

    /**
     * 根据角色ID 查询对应的信息 回显
     *
     * @param roleId
     * @return
     */
    @GetMapping("/{roleId}")
    @ApiOperation("根据角色id查询对应的信息")
    @PreAuthorize("@ss.hasPermi('system:role:query')")
    public AjaxResult FindRoleByRoleId(@PathVariable Long roleId) {
        SysRole role = sysRoleService.findRoleByRoleId(roleId);
        log.info("查询了id为{},信息为{}", roleId, role);
        return AjaxResult.success(role);
    }

    /**
     * 获取角色选择框列表
     *
     * @return
     */
    @GetMapping("/optionselect")
    @ApiOperation("获取角色选择框列表")
    @PreAuthorize("@ss.hasPermi('system:role:query')")
    public AjaxResult optionSelect() {
        log.info("获取了角色的选择框列表数据");
        return AjaxResult.success(sysRoleService.optionSelect());
    }

    /**
     * 修改保存角色数据权限信息
     *
     * @param role
     * @return
     */
    @PutMapping("/dataScope")
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @ApiOperation("修改保存角色数据权限信息")
    public AjaxResult dateScope(@RequestBody SysRole role) {

        iSysRoleService.dateScope(role);
        return AjaxResult.success();
    }

    /**
     * export
     * @param role
     * @return
     */
    @GetMapping("/export")
    @ApiOperation("export")
    public AjaxResult export(SysRole role) {

        List<SysRole> list = iSysRoleService.selectRoleList(role);
        ExcelUtil<SysRole> util = new ExcelUtil<>(SysRole.class);
        return util.exportExcel(list, "角色数据");
    }


    /**
     * 分页查询角色列表
     *
     * @param role
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("分页查询角色列表")
    public TableDataInfo list(SysRole role) {

        startPage();
        List<SysRole> list = iSysRoleService.selectRoleList(role);
        //封装TableDataInfo对象数据
        TableDataInfo tableDataInfo = new TableDataInfo<>();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("操作成功");
        tableDataInfo.setRows(list);
        tableDataInfo.setTotal(new PageInfo(list).getTotal());
        return tableDataInfo;
    }

}
